let xiaoxin = {

	xxClick: (Nodemode, Nodename, Timeout,flag) => {

			if(!Nodename) return toast("节点名不能为空")
			if(Nodename.includes("|")){
				Nodename = Nodename.split("|")
				Nodename.forEach((item,index)=>{
					let node0 = Nodemode(item).getOneNodeInfo(Timeout);
					if (node0) {
						let centerx0 = Math.ceil((node0.bounds.left + node0.bounds.right) / 2)
						let centery0 = Math.ceil((node0.bounds.top + node0.bounds.bottom) / 2)
						logd('当前点击坐标' + centerx0 + ',' + centery0)
						if(flag) clickPoint(centerx0, centery0)
						return true
					}else{
						return false
					}
				})
			}else
			{
				let node1 = Nodemode(Nodename).getOneNodeInfo(Timeout);
				if (node1) {
					let centerx1 = Math.ceil((node1.bounds.left + node1.bounds.right) / 2)
					let centery1 = Math.ceil((node1.bounds.top + node1.bounds.bottom) / 2)
					logd('当前点击坐标' + centerx1 + ',' + centery1)
					if(flag) clickPoint(centerx1, centery1)
					return true
				}else{
					return false
				}
			}





	},
	findPic: (fileName,Similarity,interval,timeout) => {
		// 设置图片延迟
		image.setInitParam({"action_timeout": 5000, "auto_click_request_dialog": false});
		image.setFindColorImageMode(2);
		let Executiontime = 0
		let  request = image.requestScreenCapture(10000,0);
		if(!request) {
			toast('截图申请失败');
			exit()
		}
		let  d= image.initOpenCV();
		//申请完权限至少等1s(垃圾设备多加点)再截图,否则会截不到图
		sleep(1000)
		//从工程目录下res文件夹下读取sms.png文件
		for(let i =1; i<=100; i++){
			let sms=readResAutoImage(fileName);
			//抓取屏幕
			let aimage = image.captureFullScreen();
			if (aimage != null) {
				//在图片中查找
				let points = image.findImage(aimage, sms,0, 0, 0, 0,0.7, Similarity, 1, 5);
				logd('循环查找中' + ' ' + JSON.stringify(points))
				//这玩意是个数组
				if(points && points.length> 0){
					for(let i=0;i<points.length;i++){
						//logd(points[i])
						let x = parseInt((points[i].left + points[i].right)/2)
						let y = parseInt((points[i].top + points[i].bottom)/2)
						//点击坐标
						clickPoint(x,y)
						return
					}
				}
			}
			sleep(interval);
			Executiontime += interval
			if(Executiontime >= timeout){
				logd('循环结束，无结果')
				return
			}
		}
		//图片要回收
		image.recycle(aimage)
	},


	//多点找色函数封装
	findMiColor:(first,ManyPoints,Similarity,interval,timeout)=>{
		// 设置图片延迟
		image.setInitParam({"action_timeout": 5000, "auto_click_request_dialog": false});
		image.setFindColorImageMode(2);
		let Executiontime = 0
		let  request = image.requestScreenCapture(10000,0);
		if(!request) {
			toast('截图申请失败');
			exit()
		}
		let  d= image.initOpenCV();
		//申请完权限至少等1s(垃圾设备多加点)再截图,否则会截不到图
		sleep(1000)
		for(let i =1; i<=100; i++){
			let  aimage = image.captureFullScreen();
			let  points = image.findMultiColor(aimage, first,ManyPoints, Similarity, 0, 0, 0, 0, 1,1);
			//logd("points " + JSON.stringify(points));
			//这玩意是个数组
			if(points && points.length> 0){
				for(let i=0;i<points.length;i++){
					logd(points[i],points[i].x,points[i].y)
					//点击坐标
					clickPoint(points[i].x,points[i].y)
					logd('点击成功')
					return
				}
			}
			sleep(interval);
			Executiontime += interval
			if(Executiontime >=timeout){
				logd('循环查找结束,无结果')
				return
			}
		}
		//图片要回收
		image.recycle(aimage)
	},

	// OCR找字封装多字查找
	ocrFindWord:(searchLabel,interval,timeout,flag)=>{
		logd("isServiceOk " + isServiceOk());
		if(!searchLabel){
			return toast('查找字符不能为空');
		}else if(searchLabel.includes("|")){
			searchLabel = searchLabel.split("|")
		}
		if(Array.isArray(searchLabel)){
			logd(searchLabel);
			startEnv()
			let Executiontime = 0
			let request = image.requestScreenCapture(10000, 0);
			if (!request) {
				request = image.requestScreenCapture(10000, 0);
			}
			logd("申请截图结果... " + request)
			if (!request) {
				loge("申请截图权限失败,检查是否开启后台弹出,悬浮框等权限")
				exit()
			}
			//申请完权限至少等1s(垃圾设备多加点)再截图,否则会截不到图
			sleep(1000)
			let mlkit = {
				"type": "mlkit"
			}
			let inited = ocr.initOcr(mlkit)
			logd("初始化结果 -" + inited);
			if (!inited) {
				loge("error : " + ocr.getErrorMsg());
				return;
			}
			for(let i =1; i<=100; i++){
				let tmpImage = image.captureFullScreen();
				// orz 参数代表是旋转角度，0 代表不旋转 90 代表向左旋转90度，还有180，270，360参数
				let result = ocr.ocrImage(tmpImage, 20000, {"orz": 0});
				logd(JSON.stringify(result))
				result = result.filter((item,index)=>{
					// 使用正则表达式匹配汉字
					let chineseCharacters = item.label.match(/[\u4e00-\u9fa5]+/g);
					// 如果匹配到汉字，更新item.label，否则过滤掉该项
					if (chineseCharacters) {
						item.label = chineseCharacters.join();
					}
					return item
				})


				logd(JSON.stringify(result))
				let  res = result.find((item)=>{
					return searchLabel.includes(item.label)
				})
				logd(res)
				if(res){
					if(flag) clickPoint(res.x + (res.width / 2),res.y + (res.height / 2))
					return true
				}else {
					logd('循环查找中 ' + res)
				}
				sleep(1000);
				Executiontime+=interval

				if(Executiontime >=timeout){
					logd('循环结束，无结果')
					return
				}
			}

		}else{
			startEnv()
			let Executiontime = 0
			let request = image.requestScreenCapture(10000, 0);
			if (!request) {
				request = image.requestScreenCapture(10000, 0);
			}
			logd("申请截图结果... " + request)
			if (!request) {
				loge("申请截图权限失败,检查是否开启后台弹出,悬浮框等权限")
				exit()
			}
			//申请完权限至少等1s(垃圾设备多加点)再截图,否则会截不到图
			sleep(1000)
			let mlkit = {
				"type": "mlkit"
			}
			let inited = ocr.initOcr(mlkit)
			logd("初始化结果 -" + inited);
			if (!inited) {
				loge("error : " + ocr.getErrorMsg());
				return;
			}
			for(let i =1; i<=100; i++){
				let tmpImage = image.captureFullScreen();
				// orz 参数代表是旋转角度，0 代表不旋转 90 代表向左旋转90度，还有180，270，360参数
				let result = ocr.ocrImage(tmpImage, 20000, {"orz": 0});
				logd(JSON.stringify(result))
				let  res = result.find((item)=>{
					return item.label == searchLabel
				})

				if(res){
					if(flag) clickPoint(res.x + (res.width / 2),res.y + (res.height / 2))
					return true
				}else {
					logd('循环查找中 ' + res)
				}
				sleep(1000);
				Executiontime+=interval

				if(Executiontime >=timeout){
					logd('循环结束，无结果')
					return
				}
			}
		}
		image.recycle(tmpImage)
		//释放所有资源
		ocr.releaseAll();

	},

	//多文本查找
	xxClick1: (RegtextName,flag,timeout) => {
		// 记录开始时间
		let StartTime = 0
		let isClick = false
		if(!RegtextName){
			return toast('规则不能为空');
		}else if(RegtextName.includes("|")){
			RegtextName = RegtextName.split("|")
		}

		while(true){
			sleep(200);
			if(StartTime >=timeout){
				logd("匹配超时")
				break
			}
			if(Array.isArray(RegtextName)){
				RegtextName = RegtextName.map(item=> "^.*"+item+"$")
				RegtextName.forEach((item,index)=>{
					let selector = textMatch(item);
					let result = click(selector);
					if (result && flag) {
						logd("点击成功");
						isClick = true
					}else if(result && !flag){
						logd("节点存在");
					}
					else {
						StartTime +=200
						logd("点击失败");
					}
				})
			}else{
				let selector = textMatch(RegtextName);
				let result = click(selector);
				if (result && flag) {
					logd("点击成功");
					isClick = true
				}else if(result && !flag){
					logd("节点存在");
				}
				else {
					StartTime +=200
					toast("点击失败");
				}

			}
			if(isClick) break


		}






	},
}


module.exports = xiaoxin